package it.amicosmanettone.whatsimage.client;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import it.amicosmanettone.archive.ArchiveManager;

import it.amicosmanettone.whatsimage.Configuration;
import it.amicosmanettone.whatsimage.HttpClientThread;
import it.amicosmanettone.whatsimage.MainActivity;
import it.amicosmanettone.whatsimage.jsonbean.Catalogue;
import it.amicosmanettone.whatsimage.jsonbean.Category;
import it.amicosmanettone.whatsimage.jsonbean.CheckAppVersion;
import it.amicosmanettone.whatsimage.jsonbean.CheckFirstMessage;
import it.amicosmanettone.whatsimage.jsonbean.CheckMessage;
import it.amicosmanettone.whatsimage.jsonbean.ContentList;
import it.amicosmanettone.whatsimage.jsonbean.Image;
import it.amicosmanettone.whatsimage.jsonbean.UserCredit;

public class ApiClient {
	
	
	static String BASE_URL = Configuration.API_URL;
	
	public static String API_CheckAppVersion(String currentVersion){
		HttpClientThread client=new HttpClientThread(BASE_URL, "CheckAppVersion.json");
		client.start();
		synchronized (client) {
			try {
				client.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String json=HttpClientThread.threadOutput;
		Log.v("JSON", json);
		
		Gson gson = new Gson();
		CheckAppVersion mes= gson.fromJson(json, CheckAppVersion.class);
		return(mes.getMessage());
	}
	
	public static ContentList API_GetContentList(String categoryLabel, String ordering){
		//Ordering: NORMAL, RANKING, DATE, CASUAL
		ContentList list=new ContentList();
		Image image1=new Image();
		image1.setCreationDate(System.currentTimeMillis());
		image1.setDescription("Bimbo che ride");
		image1.setFilename("image1.jpg");
		image1.setFullPath("http://www.amicosmanettone.it/api/whatsimage/repository/image1.jpg");
		image1.setLabel("Bimbo");
		image1.setPreview("http://www.amicosmanettone.it/api/whatsimage/repository/pre_image1.jpg");
		image1.setSource("H4cK3r");
		image1.setType("");
		image1.setRating(4);
		
		Image image2=new Image();
		image2.setCreationDate(System.currentTimeMillis());
		image2.setDescription("Bart Simpson");
		image2.setFilename("image2.jpg");
		image2.setFullPath("http://www.amicosmanettone.it/api/whatsimage/repository/image2.jpg");
		image2.setLabel("Simpson");
		image2.setPreview("http://www.amicosmanettone.it/api/whatsimage/repository/pre_image2.jpg");
		image2.setSource("Cr4cK3r");
		image2.setType("PREMIUM");
		image2.setRating(5);
		
		Image image3=new Image();
		image3.setCreationDate(System.currentTimeMillis());
		image3.setDescription("Bart Simpons che nuota per recuperare du spicci");
		image3.setFilename("image3.jpg");
		image3.setFullPath("http://www.amicosmanettone.it/api/whatsimage/repository/image3.jpg");
		image3.setLabel("Simpson");
		image3.setPreview("http://www.amicosmanettone.it/api/whatsimage/repository/pre_image3.jpg");
		image3.setSource("Cr4cK3r");
		image3.setType("PREMIUM");
		image3.setRating(2);
		
		List imagesList=new ArrayList();
		imagesList.add(image1);
		imagesList.add(image2);
		imagesList.add(image3);
		
		
		list.setCategoryLabel("SIMPSON");
		list.setCategoryName("I Simpson");
		list.setResultCode("OK");
		list.setImages(imagesList);
		return(list);
		
	}
	
	public static UserCredit API_GetUserCredit(){
		UserCredit credit=new UserCredit();
		credit.setCredit(100L);
		return(credit);
	}
	
	
	
	public static CheckFirstMessage API_CheckFirstMessage(){
		CheckFirstMessage result=new CheckFirstMessage();
		return (result);
	}//End method...
	
	public static CheckMessage API_CheckMessageForUser(){
		CheckMessage result=new CheckMessage();
		return (result);
	}//End method...
	
	
	public static Catalogue API_GetCatalogue(){
		Catalogue catalogue=new Catalogue();
		catalogue.setApi("GetCatalogue");
		catalogue.setResultCode("OK");
		
		Category a=new Category();
		a.setCategoryLabel("ANIMALI");
		a.setCategoryName("Animali");
		a.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/cuccioli.jpg");
		
		List foglie=new ArrayList();
		
		Category foglia1=new Category();
		foglia1.setCategoryLabel("GATTI");
		foglia1.setCategoryName("GATTI name");
		foglia1.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/pre_image1.jpg");
		Category foglia2=new Category();
		foglia2.setCategoryLabel("CANI");
		foglia2.setCategoryName("CANI name");
		foglia2.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/pre_image1.jpg");
		
		
		foglie.add(foglia1);
		foglie.add(foglia2);
		a.setLeaf(foglie);
		
		Category b=new Category();
		b.setCategoryLabel("SIMPSON");
		b.setCategoryName("I Simpson");
		b.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/simpson.jpg");
		
		
		Category c=new Category();
		c.setCategoryLabel("DIVERTENTI");
		c.setCategoryName("Divertenti");
		c.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/divertenti.jpg");
		
		Category d=new Category();
		d.setCategoryLabel("UOMINIDONNE");
		d.setCategoryName("Uomini VS Donne");
		d.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/mogliemarito.png");
		
		Category e=new Category();
		e.setCategoryLabel("DEMENZIALI");
		e.setCategoryName("Demenziali");
		e.setCategoryImage("http://www.amicosmanettone.it/api/whatsimage/repository/demenziali.jpg");
		
		List lista=new ArrayList();
		lista.add(a);
		lista.add(b);
		lista.add(c);
		lista.add(d);
		lista.add(e);
		
		catalogue.setCategory(lista);
		return(catalogue);
	}
	
	public static boolean getImage(ImageView imageView,String fileUrl){
		
				
				 DownloadTask downloadThread= new DownloadTask(imageView, fileUrl);		
			
		
		
		return(true);
	}
	
	
	
	
	
	
}
